package cn.xuchunh.campusbbs.storage.sqlite;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.bean.Member;
import cn.xuchunh.campusbbs.bean.Reply;

/**
 * Created by XuChunH on 2015/10/28.
 */
public class DBManager {

    private static DBManager instance;
    private static DBHelper dbHelper;

    public synchronized static DBManager getInstance() {
        if (instance == null) {
            instance = new DBManager();
            dbHelper = new DBHelper(AppContext.getInstance());
        }
        return instance;
    }

    private DBManager() {
    }

    public long insertReplyDetail(int topicId, int replyId, String content) {
        if (TextUtils.isEmpty(content)) {
            return 0;
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        if (db.isOpen()) {
            String sql = "replace into " + DataBaseConstant.TABLE_REPLY_DETAIL + "("
                    + DataBaseConstant.COLUMN_TOPIC_ID + ","
                    + DataBaseConstant.COLUMN_REPLY_ID + ","
                    + DataBaseConstant.COLUMN_REPLY_CONTENT + ") values(?,?,?)";
            SQLiteStatement statement = db.compileStatement(sql);
            statement.bindLong(1, topicId);
            statement.bindLong(2, replyId);
            statement.bindString(3, content);
            long rawId = statement.executeInsert();
            statement.close();
            return rawId;
        }
        return 0;
    }

    public void insertReplyDetail(List<Reply> list) {

        if (list == null || list.size() < 1) {
            return;
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            String sql = "replace into" + DataBaseConstant.TABLE_REPLY_DETAIL
                    + "("
                    + DataBaseConstant.COLUMN_REPLY_ID + ","
                    + DataBaseConstant.COLUMN_REPLY_CONTENT + ")"
                    + " values(?,?)";
            SQLiteStatement statement = db.compileStatement(sql);
            db.beginTransaction();
            for (Reply item : list) {
                statement.bindLong(1, item.getUserId());
                statement.bindString(2, item.getHtmlFace());
                statement.executeInsert();
            }
            db.setTransactionSuccessful();
            db.endTransaction();
            statement.close();
        }

    }

    public String loadReplyContent(int topicId, int replyId) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        if (database.isOpen()) {
            Cursor cursor = database.query(DataBaseConstant.TABLE_REPLY_DETAIL,
                    new String[]{DataBaseConstant.COLUMN_REPLY_ID, DataBaseConstant.COLUMN_REPLY_CONTENT},
                    DataBaseConstant.COLUMN_TOPIC_ID + "=" + topicId + " and " + DataBaseConstant.COLUMN_REPLY_ID + "=" + replyId,
                    null, null, null, null);
            if (cursor.moveToFirst()) {
                return cursor.getString(cursor.getColumnIndexOrThrow(DataBaseConstant.COLUMN_REPLY_CONTENT));
            }
        }
        return "";
    }

    public void insertLoginedUser(String userName, int userId, String avatar) {
        if (!TextUtils.isEmpty(userName)) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            if (db.isOpen()) {
                String sql = "replace into " + DataBaseConstant.TABLE_LOGINED_USER + "("
                        + DataBaseConstant.COLUMN_USER_NAME + ","
                        + DataBaseConstant.COLUMN_USER_ID + ","
                        + DataBaseConstant.COLUMN_USER_AVATAR + ") values(?,?,?)";
                SQLiteStatement statement = db.compileStatement(sql);
                statement.bindString(1, userName);
                statement.bindLong(2, userId);
                statement.bindString(3, avatar);
                statement.executeInsert();
                statement.close();
            }
        }
    }

    public void insertLoginedUser(Member member) {
        insertLoginedUser(member.getUserName(), member.getUserId(), member.getAvatar());
    }

    public List<Member> loadLoginedUsers() {
        List<Member> memberList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query(DataBaseConstant.TABLE_LOGINED_USER,
                    new String[]{DataBaseConstant.COLUMN_USER_NAME, DataBaseConstant.COLUMN_USER_ID, DataBaseConstant.COLUMN_USER_AVATAR},
                    null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    Member member = new Member();
                    member.setUserName(
                            cursor.getString(cursor.getColumnIndexOrThrow(DataBaseConstant.COLUMN_USER_NAME)));
                    member.setUserId(
                            cursor.getInt(cursor.getColumnIndexOrThrow(DataBaseConstant.COLUMN_USER_ID)));
                    member.setAvatar(
                            cursor.getString(cursor.getColumnIndexOrThrow(DataBaseConstant.COLUMN_USER_AVATAR)));
                    memberList.add(member);
                } while (cursor.moveToNext());
            }
        }
        return memberList;
    }

    public synchronized static void close() {
        if (instance != null) {
            dbHelper.close();
            dbHelper = null;
            instance = null;
        }
    }

}
